<?php
namespace Admin\Controller;

class TurndomlogController extends AdminBaseController
{
   public function index()
    {
        $this->display('turndomlog/index');
    }
    
    public function turnlist(){
        if(session('auid')==null){
            $this->display('index/login');
            exit();
        }
         
        $uid=I('uid','');
        if($uid!=''){
            $condi['a.uid']=$uid;
        }
        $status=I('status','');
        if($status!=''){
            $condi['a.status']=$status;
        }
        $domain=I('domain','');
        if($domain!=''){
            $condi['a.domain']=array('like', '%'.$domain);
        }
        $page=I('page',1);
        $rows=I('rows',50);
        $start=($page-1)*$rows;
        
        $DomainTurnoutlog = M('DomainTurnoutlog');
        $list = $DomainTurnoutlog->join('as a left join dd_register_business as b on a.regi_buines_id=b.id ')
        ->field('a.id,a.uid,a.status,a.admin_remark,a.createdate,a.domain,a.pushid,b.name')
        ->where($condi)->order("a.createdate desc")->limit($start,$rows)->select();
        
        $count = $DomainTurnoutlog->join('as a left join dd_register_business as b on a.regi_buines_id=b.id')
        ->where($condi)->count();
        
        $map=array(
            'rows'=>$list,
            'total'=>$count
        );
        $this->ajaxReturn($map,'JSON');
    }
    
    public function check(){
        $id=I('id','');
        $status=I('status','');
        $jieguo=I('jieguo','');
        if($id=='' || $status==''){
            $map=array(
                'status'=>2,
                'msg'=>'ID不存在'
            );
            $this->ajaxReturn($map,'JSON');
            exit();
        }
       $turnlog = M('DomainTurnoutlog')->field('id,status,udomainid')->find($id);
       if(empty($turnlog) || $turnlog[0]['status']>1){
           $map=array(
               'status'=>2,
               'msg'=>'无需处理'
           );
           $this->ajaxReturn($map,'JSON');
           exit();
       }
       if($status==3){//转出成功后此域名将从平台被删除
           M('DomainTurnoutlog')->where('id='.$id)->setField(array('status'=>3,'admin_remark'=>$jieguo));
           M('UserDomains')->where('id='.$turnlog['udomainid'])->delete();
       }else if($status==2){
           M('DomainTurnoutlog')->where('id='.$id)->setField(array('status'=>2,'admin_remark'=>$jieguo));
           M('UserDomains')->where('id='.$turnlog['udomainid'])->setField(array('trans_status'=>1));
       }
        $map=array(
            'status'=>1,
            'msg'=>''
        );
        $this->ajaxReturn($map,'JSON');
        exit();
    }
    
    public function jieshou(){
        $id=I('id','');
        $turnlog = M('DomainTurnoutlog')->field('id,status,udomainid')->find($id);
        if(empty($turnlog) || $turnlog[0]['status']>1){
            $map=array(
                'status'=>2,
                'msg'=>'无需处理'
            );
            $this->ajaxReturn($map,'JSON');
            exit();
        }
        if($turnlog['status']==1){
            M('DomainTurnoutlog')->where('id='.$id)->setField(array('status'=>4));
        }
        $map=array(
            'status'=>1,
            'msg'=>''
        );
        $this->ajaxReturn($map,'JSON');
        exit();
    }
}